import os
import mlib_utils
import xmmsclient.glib
from album_man import AlbumManager
from mlib_broadcastman import BroadcastManager
from gtk import *
from gtk.glade import XML
from xmmsclient import XMMS

class MLib(XML):
    def __init__(self, xmms2):
        XML.__init__(self, "mlib.glade")
        self.signal_autoconnect(self)
         #mlib_utils.build_medialib(self)
        iconview = self.get_widget("iconview1")
        model = ListStore(gdk.Pixbuf, str, str)
        iconview.set_model(model)
        self.am = AlbumManager(self, xmms2)
        self.findbox = self.get_widget("find_box")
        self.infobox = self.get_widget("info_box")
        self.iconbox = self.get_widget("iconview_box")
        self.model = model
        self.xmms2 = xmms2
        BroadcastManager(self, xmms2)
        

    def hide_findbox(self, *args):
        self.findbox.hide()

    def show_album_details(self, iconview, path):
        p, = path
        pixbuf, markup, album = self.model[p]
        self.am.set_album(pixbuf, album)
        self.iconbox.hide()
        self.infobox.show()
        
        
    def show_findbox(self, *args):
        iconbox_isvisible = self.iconbox.get_property("visible")
        if iconbox_isvisible:
            self.findbox.show()
            findbox_entry = self.get_widget("findbox_entry")
            findbox_entry.grab_focus()
        
    def show_iconview(self, button):
        self.infobox.hide()
        self.iconbox.show()

    def quit(self, *args):
        main_quit()


xmms2 = XMMS("mlib")
try:
    xmms2.connect(os.getenv("XMMS_PATH"))
except Exception, e:
    print "[xmms2 client]: Could not connect to xmms2d"
    print e
    print "Aborting!"
    exit(1)
xmmsclient.glib.GLibConnector(xmms2)

MLib(xmms2)
main()
